package com.smarthome.web.property.controller.messageTask;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.common.tools.SpringUtils;
import com.smarthome.core.messageTask.MessageTaskService;
import com.smarthome.domain.vo.boss.TaskMessageVo;
import com.smarthome.web.property.utils.UserHolder;

/**
 * 小区查询控制类
 * @author cyj
 *
 */
@Controller
@RequestMapping("/messageTask")
public class MessageTaskController {

	private static Set<MessageTaskService> messageTaskServices = new HashSet<MessageTaskService>();
	
	/***
	 * @title 初始化数据
	 * @createTime 2016年3月29日 下午10:55:07
	 * @author dhc
	 */
	@PostConstruct
	private void initTaskService(){
		String[] beans = SpringUtils.getBeanFactory().getBeanDefinitionNames();
		Class<?> clazz = MessageTaskService.class;
		for(String beanName : beans){
			Object obj = SpringUtils.getBean(beanName);
			if(clazz.isAssignableFrom(obj.getClass())){
				messageTaskServices.add((MessageTaskService)obj);
			}
		}
	}
		

    /**
     * 
     * @title 获取系统消息
     * @createTime 2016年3月29日 下午10:00:21
     * @author dhc
     */
    @RequestMapping("/messages")
    @ResponseBody
    public List<TaskMessageVo>  getList() {
    	 List<TaskMessageVo> tasks = new ArrayList<TaskMessageVo>();
    	
    	for(MessageTaskService service : messageTaskServices){
    		TaskMessageVo vo = service.getTaskMessage(UserHolder.getCurrentResidenceId());
    		if(null != vo && null != vo.getCount() && vo.getCount().longValue() >0 ){
    			tasks.add(service.getTaskMessage(UserHolder.getCurrentResidenceId()));
    		}
    	}
    	return tasks;
    }
    
   
}
